package pkj.no.notetab.gui;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class TopMenu extends JMenuBar implements ActionListener {

	private JMenu mFile = new JMenu("File");
	private JMenuItem mFileNew = new JMenuItem("New");
	private JMenuItem mFileSave = new JMenuItem("Save");
	private JMenuItem mFileSaveAs = new JMenuItem("Save as..");
	private JMenuItem mFileSaveAll = new JMenuItem("Save all");
	private JMenuItem mFileOpen = new JMenuItem("Open ..");
	private JMenuItem mFileQuit = new JMenuItem("Quit");
	
	
	private JMenu mText = new JMenu("Text");
	private JMenu mFonts = new JMenu("Font");
	private JMenu mFontSize = new JMenu("Font size");
	
	final private int fontSizes[] = {8,12,14,16,18,20,24,28,32,36,48,52,72};
	
	
	
	
	public Tabs tabs;

	public TopMenu(final Tabs tabs) {

		this.tabs = tabs;

		mFileNew.addActionListener(this);
		mFileSaveAs.addActionListener(this);
		mFileOpen.addActionListener(this);
		mFileQuit.addActionListener(this);
		mFileSave.addActionListener(this);
		mFileSaveAll.addActionListener(this);
		
		mFileSave.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		// Add File menu.
		mFile.add(mFileNew);
		mFile.add(mFileSave);
		mFile.add(mFileSaveAs);
		mFile.add(mFileSaveAll);
		mFile.add(mFileOpen);
		mFile.add(mFileQuit);
		
		
		mText.add(mFonts);
		mText.add(mFontSize);
		
		
		// Populate Fonts.
		Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();

		ButtonGroup g = new ButtonGroup();
		for(final Font f : fonts){
			
			JRadioButtonMenuItem r = new JRadioButtonMenuItem(f.getName());
			r.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					Settings.font = f;
					for(Tab t : tabs.getTabs()){
						t.getTextHolder().setFont(f.deriveFont(Settings.fontSize));
						
					}
				}});
			mFonts.add(r);
			g.add(r);
		}
		g = new ButtonGroup();
		for(final int fSize : fontSizes){
			JRadioButtonMenuItem r = new JRadioButtonMenuItem(String.valueOf(fSize));
			r.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					Settings.fontSize = (float)fSize;
					for(Tab t : tabs.getTabs()){
						t.getTextHolder().setFont(t.getTextHolder().getFont().deriveFont(Settings.fontSize));
					}
				}});
			g.add(r);
			mFontSize.add(r);
		}
		
		
		
		
		add(mFile);
		add(mText);

	}

	@Override
	public void actionPerformed(ActionEvent ev) {
		// We might want to override this.
		boolean saveAs = false;
		
		if (ev.getSource() == mFileSave){
			Tab tab = tabs.getActiveTab();
			try {
				tab.save();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
		if (ev.getSource() == mFileSaveAll){
			for(Tab t : tabs.getTabs()){
				try {
					t.save();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		if (ev.getSource() == mFileSaveAs) {
			Tab tab = tabs.getActiveTab();
			try {
				tab.saveAs();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		if (ev.getSource() == mFileOpen) {
			final JFileChooser fc = new JFileChooser();
			int returnVal = fc.showOpenDialog(TopMenu.this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File f = fc.getSelectedFile();
				try {
					tabs.fileToTab(f);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		if (ev.getSource() == mFileNew) {

		}
		if (ev.getSource() == mFileQuit) {
			
			System.exit(0);			
		}
	}

}
